import {loadBMap} from "@/utils/map.js";
import axios from "axios";

export default {
    data() {
        return {
            BMap: null,
            geolocation: null, // Geolocation对象实例
            positioning: false, // 定位中
            positioningInterval: null, // 定位倒计时计时器
            countDown: 30, // 倒计时，单位秒
            location: null, // 位置信息
            province: "", // 经度
            city: "", // 纬度
            address: "",
            //区
            district: ""
        };
    },
    destroyed() {
        this.flag = false
    },
    mounted() {
        const _this = this;
        _this.BMap = null
        window.initBaiduMapScript = () => {
            _this.BMap = window.BMap;
            console.log("_this.BMap", _this.BMap)
            _this.getLocationMap();
        };
        loadBMap("initBaiduMapScript");

    },
  // beforeUpdate() {
  //   this.getLocation();
  // },
  watch: {
    // '$route': 'getLocationMap'
  },
activated() {
  window.initBaiduMapScript = () => {
    this.BMap = window.BMap;
    console.log("_this.BMap", this.BMap)
    this.getLocationMap();
  };
  loadBMap("initBaiduMapScript");
},
  methods: {
    // city(){
		// 	map.init().then(BMap =>{
		// 		const locationCity = new BMap.Geolocation();
		// 		var that = this;
		// 		locationCity.getCurrentPosition(
		// 			function getinfo(options){
		// 				let city = options.address.city;
		// 				that.LocationCity = city;
		// 				console.log(options)
		// 				alert(city)
		// 				alert(`${options.latitude}` + `  ` +`${options.longitude}`)
		// 				alert(`${options.point.lng}` + `  ` +`${options.point.lat}`)
		// 			},
		// 			function (e){
		// 				that.LocationCity = '定位失败';
		// 				console.log('定位失败')
		// 			},
		// 			{provider:'baidu'}
		// 			)
		// 	})
		// }
	
    // 获取地理定位
    getLocationMap() {
      const _this = this;
      _this.geolocation = new _this.BMap.Geolocation();
      // _this.geolocation = new _this.BMap.LocalCity();
      console.log("_this", _this.geolocation)
      if (_this.geolocation) {
        // 开启SDK辅助定位，仅当使用环境为移动web混合开发，且开启了定位sdk辅助定位功能后生效
        _this.geolocation.enableSDKLocation();
        // 开始定位
        _this.positioning = true;
        // 倒计时
        _this.positioningInterval = setInterval(() => {
          if (_this.countDown === 0) {
            _this.countDown = 30;
            clearInterval(_this.positioningInterval);
          } else {
            _this.countDown--;
          }
        }, 1000);
        // 位置选项
        const positionOptions = {
          enableHighAccuracy: true, // 要求浏览器获取最佳结果
          timeout: 30, //    超时时间
          maximumAge: 0, // 允许返回指定时间内的缓存结果。如果此值为0，则浏览器将立即获取新定位结果
        };
        // 获取用户位置信息
        _this.geolocation.getCurrentPosition((position) => {
          _this.resetPositioning();
          // 获取定位结果状态码
          const statusCode = _this.geolocation.getStatus();
          let msg = "由于未知错误而无法检索设备的位置"; // 提示消息
          // let msgType = "error"; // 消息类型
          // 判断结果状态码，为0代表获取成功，读取省市、经纬度
          switch (statusCode) {
            case 0:
              // msgType = "success";
              msg = "获取地理位置定位请求成功";
              console.log("position", position)
              if (position) {
                // 数据变量定义
                let lat = 0.0; // 经度
                let lng = 0.0; // 纬度
                // // let province = "未知"; // 经度
                // // let city = "未知"; // 纬度

                // // 坐标
                if (position.point) {
                  lat = position.point.lat;
                  lng = position.point.lng;
                }
                // var map = new BMap.Map("l-map");
                // map.centerAndZoom(new BMap.Point(lng, lat), 11);
                var myGeo = new _this.BMap.Geocoder({
                  extensions_town: true
                });
                console.log(position.point)
                // 根据坐标得到地址描述    
                myGeo.getLocation(new BMap.Point(lng, lat), function (result) {

                                    console.log("result", result)
                                    if (result) {
                                        // alert(result.address);
                                        _this.province = result.addressComponents.province;
                                        _this.city = result.addressComponents.city;
                                        _this.address = result.address;
                                        _this.district = result.addressComponents.district;
                                        console.log(_this.province, _this.city)
                                        _this.$set(_this.submitList, 'user_address', _this.province + _this.city + _this.district)
                                            /* _this.$set(_this.submitList, 'user_address', _this.address) */
                                            // new BMap.Point(position.point)
                                        position = null
                                        console.log(position);
                                        _this.flag = true
                                    }
                                });

                // 位置
                // if (position.address) {
                //   province = position.address.province;
                //   city = position.address.city;
                // }
                // _this.location = {
                //   省份: province,
                //   城市: city,
                //   经度: lat,
                //   纬度: lng,
                // };
                // console.log("submitList", _this.submitList)
                // console.log("_this.location", _this.location)

              } else {

                msg = "由于未知错误而无法检索设备的位置";
              }
              break;
            case 2:
              msg = "由于未知错误而无法检索设备的位置";
              break;
            case 4:
            case 5:
              msg = "位置服务请求非法";
              break;
            case 6:
              msg = "应用程序没有使用位置服务的权限";
              break;
            case 7:
              msg = "网络不可用或者无法连接到获取位置信息的卫星";
              break;
            case 8:
              msg = "无法在指定的最大超时间隔内检索位置信息";
              break;
            default:
              msg = "由于未知错误而无法检索设备的位置";
              break;
          }

          // console.log(" _this.$notification", _this.$toast)
          _this.$toast(msg)
          // _this.$notification[]({
          //   key: NotificationKey,
          //   message: "提示",
          //   description: msg,
          // });
        }, positionOptions);
      } else {
        _this.$toast("您的浏览器不支持地理位置服务")
        // _this.$notification.error({
        //   key: NotificationKey,
        //   message: "提示",
        //   description: "您的浏览器不支持地理位置服务",
        // });
      }
    },
    //重置定位相关数据
    resetPositioning() {
      this.positioning = false; // 定位中
      this.location = null;
      this.province = "";
      this.city = "";
      this.$set(this.submitList, 'user_address', "")
      this.countDown = 30;
      clearInterval(this.positioningInterval);
    },
  },
};
